package org.footito.android.tools;

import java.util.GregorianCalendar;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class Settings {
	public static final int CHECKVERSION = 8;

	private class Preferences {
		public static final String NICKNAME = "nickname";
		public static final String TEAM_LAST_CHECK_TIME = "lastcheckteam" + CHECKVERSION;
		public static final String PREFS_NAME = "Footito";
		public static final String EVENT_LAST_CHECK_TIME = "lastchecktime" + CHECKVERSION;
		public static final String INITIALIZED = "initialized" + CHECKVERSION;
		public static final String PREFS_TIMELINEPOSITION = "timelineposition";
	}

	public static String getLastCheck(Context context) {
		SharedPreferences mSettings = context.getSharedPreferences(Preferences.PREFS_NAME, 0);
		return mSettings.getString(Preferences.EVENT_LAST_CHECK_TIME, "");
	}

	public static void setLastCheck(Context context, GregorianCalendar newDate) {
		SharedPreferences mSettings = context.getSharedPreferences(Preferences.PREFS_NAME, 0);
		Editor editor = mSettings.edit();
		editor.putString(Preferences.EVENT_LAST_CHECK_TIME, "" + (newDate.getTimeInMillis() / 1000));
		editor.commit();
	}

	public static String getNickName(Context context) {
		SharedPreferences mSettings = context.getSharedPreferences(Preferences.PREFS_NAME, 0);
		return mSettings.getString(Preferences.NICKNAME, "");
	}

	public static void setNickName(Context context, String nickname) {
		SharedPreferences mSettings = context.getSharedPreferences(Preferences.PREFS_NAME, 0);
		Editor editor = mSettings.edit();
		editor.putString(Preferences.NICKNAME, nickname);
		editor.commit();
	}

	public static void resetChecks(Context context) {
		SharedPreferences mSettings = context.getSharedPreferences(Preferences.PREFS_NAME, 0);
		Editor editor = mSettings.edit();
		editor.putString(Preferences.TEAM_LAST_CHECK_TIME, "" + 0);
		editor.putString(Preferences.EVENT_LAST_CHECK_TIME, "" + 0);
		editor.commit();
		setInitialized(context, false);
	}

	public static String getLastTeamCheck(Context context) {
		SharedPreferences mSettings = context.getSharedPreferences(Preferences.PREFS_NAME, 0);
		return mSettings.getString(Preferences.TEAM_LAST_CHECK_TIME, "");
	}

	public static void setLastTeamCheck(Context context, GregorianCalendar newDate) {
		SharedPreferences mSettings = context.getSharedPreferences(Preferences.PREFS_NAME, 0);
		Editor editor = mSettings.edit();
		editor.putString(Preferences.TEAM_LAST_CHECK_TIME, "" + (newDate.getTimeInMillis() / 1000));
		editor.commit();
	}

	public static boolean getInitialized(Context context) {
		SharedPreferences mSettings = context.getSharedPreferences(Preferences.PREFS_NAME, 0);
		return mSettings.getBoolean(Preferences.INITIALIZED, false);
	}

	public static void setInitialized(Context context, boolean initialized) {
		SharedPreferences mSettings = context.getSharedPreferences(Preferences.PREFS_NAME, 0);
		Editor editor = mSettings.edit();
		editor.putBoolean(Preferences.INITIALIZED, initialized);
		editor.commit();
	}

	public static int getTimelinePosition(Context context) {
		SharedPreferences mSettings = context.getSharedPreferences(Preferences.PREFS_NAME, 0);
		return mSettings.getInt(Preferences.PREFS_TIMELINEPOSITION, 0);
	}

	public static void setTimelinePosition(Context context, int position) {
		SharedPreferences mSettings = context.getSharedPreferences(Preferences.PREFS_NAME, 0);
		Editor editor = mSettings.edit();
		editor.putInt(Preferences.PREFS_TIMELINEPOSITION, position);
		editor.commit();
	}

	public static void incrementTimelinePositionOf(Context context, int statusAdded) {
		SharedPreferences mSettings = context.getSharedPreferences(Preferences.PREFS_NAME, 0);
		int start = mSettings.getInt(Preferences.PREFS_TIMELINEPOSITION, 0);
		Editor editor = mSettings.edit();
		editor.putInt(Preferences.PREFS_TIMELINEPOSITION, start + statusAdded);
		editor.commit();
	}
}
